package visitorMod;

import java.util.ArrayList;

public class code {
    // 抽象访问者
    interface Analyzer {
        void analyze(Code code);
    }

    // 具体访问者1：度量软件规模
    class ScaleAnalyzer implements Analyzer {
        @Override
        public void analyze(Code code) {
            for (ClassExample classExample : code.getClassList()) {
                System.out.println("Class: " + classExample.getName());
                System.out.println("Methods: " + classExample.getMethodList().size());
                System.out.println("Variables: " + classExample.getVarList().size());
            }
        }
    }

    // 具体访问者2：提取标识符名称
    class IdentifierAnalyzer implements Analyzer {
        @Override
        public void analyze(Code code) {
            for (ClassExample classExample : code.getClassList()) {
                System.out.println("Class Name: " + classExample.getName());
                for (String method : classExample.getMethodList()) {
                    System.out.println("Method Name: " + method);
                }
                for (String var : classExample.getVarList()) {
                    System.out.println("Variable Name: " + var);
                }
            }
        }
    }

    // 具体访问者3：统计代码行数
    class LineCounterAnalyzer implements Analyzer {
        @Override
        public void analyze(Code code) {
            for (ClassExample classExample : code.getClassList()) {
                System.out.println("Class: " + classExample.getName() + " Lines: " + classExample.getLines());
            }
        }
    }

    // 代码元素接口
    interface Element {
        void accept(Analyzer analyzer);
    }

    // 类元素
    class ClassExample {
        private int lines;
        private ArrayList<String> methodList;
        private ArrayList<String> varList;
        private String name;

        public ClassExample(String name, int lines) {
            this.name = name;
            this.lines = lines;
            this.methodList = new ArrayList<>();
            this.varList = new ArrayList<>();
        }

        public void addMethod(String method) {
            methodList.add(method);
        }

        public void addVar(String var) {
            varList.add(var);
        }

        public int getLines() {
            return lines;
        }

        public ArrayList<String> getMethodList() {
            return methodList;
        }

        public ArrayList<String> getVarList() {
            return varList;
        }

        public String getName() {
            return name;
        }

    }

    // 代码类，包含类元素的集合
    class Code implements Element {
        private int lines;
        private ArrayList<ClassExample> classList;

        public Code() {
            this.lines = 0;
            this.classList = new ArrayList<>();
        }

        public void addClass(ClassExample classExample) {
            classList.add(classExample);
            lines += classExample.getLines();
        }

        public int getLines() {
            return lines;
        }

        public ArrayList<ClassExample> getClassList() {
            return classList;
        }

        @Override
        public void accept(Analyzer analyzer) {
            analyzer.analyze(this);
        }
    }

}
